
#import "UIActionSheetAdditions.h"

@implementation UIActionSheet(Additions)

//
// dismiss callback
//
static UIActionSheetDismissCallback globalDismissCallback					= nil;

//
// cancel callback
//
static UIActionSheetCancelCallback globalCancelCallback						= nil;

//
// show action sheet
//
+ (UIActionSheet*)actionSheetWithTitle:(NSString*)title destructiveTitle:(NSString*)destructiveTitle buttons:(NSArray*)buttons showInView:(id)view onDismiss:(UIActionSheetDismissCallback)onDismiss onCancel:(UIActionSheetCancelCallback)onCancel
{
	[globalCancelCallback release];
	globalCancelCallback													= [onCancel copy];
	
	[globalDismissCallback release];
	globalDismissCallback													= [onDismiss copy];
	
	id<UIActionSheetDelegate> delegate										= (id<UIActionSheetDelegate>)[self class];
	UIActionSheet* actionSheet												= [[UIActionSheet alloc] initWithTitle:title delegate:delegate cancelButtonTitle:nil destructiveButtonTitle:destructiveTitle otherButtonTitles:nil];
	for(NSString* button in buttons)
		[actionSheet addButtonWithTitle:button];
	
	[actionSheet addButtonWithTitle:NSLocalizedString(@"Cancel", nil)];
	actionSheet.cancelButtonIndex											= buttons.count + (destructiveTitle ? 1 : 0);
	
	if([view isKindOfClass:[UIView class]])
		[actionSheet showInView:view];
	else if([view isKindOfClass:[UITabBar class]])
		[actionSheet showFromTabBar:view];
	else if([view isKindOfClass:[UIBarButtonItem class]])
		[actionSheet showFromBarButtonItem:view animated:YES];
	
	return [actionSheet autorelease];
}

//
// dismiss
//
+ (void)actionSheet:(UIActionSheet*)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex;
{
	if(buttonIndex == actionSheet.cancelButtonIndex)
	{
		if(globalCancelCallback)
			globalCancelCallback();
	}
	else if(globalDismissCallback)
	{
		globalDismissCallback(buttonIndex);
	}
}
@end
